import io
import os

from src.bot.services.qr_code import generate_qr_code

from ..generators import generate_uk_bill_data
from .base import BasePDFService

cur_dir = os.path.dirname(__file__)


class UKPDFService(BasePDFService):
    """Класс для генерации UK документов"""

    def __init__(self, filename: str = os.path.join(cur_dir, "uncompressed.pdf")):
        super().__init__(filename)

    def create_uk_bill(
        self,
        first_name: str,
        last_name: str,
        street: str,
        city: str,
        postal_code: str,
        with_watermark: bool = False,
    ) -> bytes:
        """
        Генерируем платежку под UK
        :param first_name: Имя
        :param last_name: Фамилия
        :param street: Название улицы
        :param city: Название города
        :param postal_code: Посткод
        :param with_watermark: Добавлять ли ватермарку
        :return Возвращает сгенерированный документ в байтах
        """

        random_data = generate_uk_bill_data()

        qrcode_bytes = generate_qr_code(random_data.get("qr_code_value"))
        self.replace_image(self.reader[0], 14, qrcode_bytes)

        if with_watermark:
            self.add_watermark(os.path.join(cur_dir, "watermark.png"))
            self.reader.delete_pages(1, 2)
            # С ватермаркой выдаем только одну страницу

        with io.BytesIO() as temp_file:
            temp_file.write(self.save_pdf_file())

            self.replace_text(
                temp_file,
                replaces={
                    "Chukwuemeka": first_name.capitalize(),
                    "Ohasiri": last_name.capitalize(),
                    "59 Albany Road": street.capitalize(),
                    "Doncaster": city.capitalize(),
                    "DN-4 0QN": postal_code.upper(),
                    "DN4 0QN": postal_code.upper(),
                    "13th Nov. 2021": random_data.get("excerpt_forming_date"),
                    "1179.90": random_data.get("electricity_year_sum"),
                    "509.65": random_data.get("gas_year_sum"),
                    "14th Oct. 2021": random_data.get("excerpt_period_start"),
                    "14th Oct 2021": random_data.get("excerpt_period_start").replace(
                        ".", ""
                    ),
                    "14th October 2021": random_data.get("excerpt_period_start_full_month"),
                    "11th November 2021": random_data.get("excerpt_period_end_full_month"),
                    "11th Nov. 2021": random_data.get("excerpt_date"),
                    "12th Nov. 2021": random_data.get("excerpt_period_end"),
                    "12th Nov 2021": random_data.get("excerpt_period_end").replace(
                        ".", ""
                    ),
                    "29th Oct. 2021": random_data.get("previous_payment_date"),
                    "29th November 2021": random_data.get("end_of_cur_month"),
                    "A-63EA3D5C": random_data.get("account_number"),
                    "$excerpt_num$": random_data.get("excerpt_num"),
                    "$electric_meter_number$": random_data.get("electric_meter_number"),
                    "$gas_meter_number$": random_data.get("gas_meter_number"),
                    "$previous_payment_sum$": random_data.get("previous_payment_sum"),
                    "$supply_number$": random_data.get("supply_number"),
                    "$supply_number1$": random_data.get("supply_number1"),
                    "$supply_number2$": random_data.get("supply_number2"),
                    "$electricity_reading_start$": random_data.get(
                        "electricity_reading_start"
                    ),
                    "$electricity_reading_end$": random_data.get(
                        "electricity_reading_end"
                    ),
                    "$electricity_used$": random_data.get("electricity_used"),
                    "$electricity_summ$": random_data.get("electricity_summ"),
                    "$electricity_summ_with_charge$": random_data.get(
                        "electricity_summ_with_charge"
                    ),
                    "$electricity_vat$": random_data.get("electricity_vat"),
                    "$total_electricity_summ$": random_data.get(
                        "total_electricity_summ"
                    ),
                    "$electricity_estimated_annual_usage$": random_data.get(
                        "electricity_estimated_annual_usage"
                    ),
                    "$gas_reading_start$": random_data.get("gas_reading_start"),
                    "$gas_reading_end$": random_data.get("gas_reading_end"),
                    "$gas_used$": random_data.get("gas_used"),
                    "$gas_energy_used$": random_data.get("gas_energy_used"),
                    "$gas_summ_with_charge$": random_data.get("gas_summ_with_charge"),
                    "$gas_vat$": random_data.get("gas_vat"),
                    "$total_gas_summ$": random_data.get("total_gas_summ"),
                    "$gas_electricity_before_vat$": random_data.get(
                        "gas_electricity_before_vat"
                    ),
                    "$gas_electricity_vat$": random_data.get("gas_electricity_vat"),
                    "$total_gas_electricity_summ$": random_data.get(
                        "total_gas_electricity_summ"
                    ),
                    "$gas_estimated_annual_usage$": random_data.get(
                        "gas_estimated_annual_usage"
                    ),
                    "$average_electricity_used$": random_data.get(
                        "average_electricity_used"
                    ),
                    "$average_gas_used$": random_data.get("average_gas_used"),
                    "7th May 2022": random_data.get("price_guaranteed_until"),
                },
            )

            temp_file.seek(0)

            return temp_file.read()
